package com.jrh.web.brand;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jrh.biz.brand.BrandService;
import com.jrh.biz.hall.HallService;
import com.jrh.biz.product.ProductService;
import com.jrh.biz.product.set.ProductSetService;
import com.jrh.dal.CategoryDao;
import com.jrh.domain.common.Page;
import com.jrh.domain.entity.Brand;
import com.jrh.domain.entity.Category;
import com.jrh.domain.entity.Hall;
import com.jrh.domain.entity.Product;
import com.jrh.domain.entity.ProductSet;

/**
 * 品牌相关控制器
 * 
 * @author yout 2012-7-22 上午11:16:12
 */
@Controller
@RequestMapping("/brand")
public class BrandController {

    private static final Integer pageSize = 5;

    @Autowired
    private BrandService         brandService;

    @Autowired
    private ProductSetService    productSetService;

    @Autowired
    private HallService          hallService;

    @Autowired
    private CategoryDao          categoryDao;

    @Autowired
    private ProductService       productService;

    @RequestMapping("/{brandId}")
    public String index(@PathVariable Long brandId, Model model, Long categoryId, Long productSetId, Integer pageIndex) {
        if (brandId == null) {
            return "/error";
        }
        // 品牌信息
        Brand brand = brandService.getBrandById(brandId);
        if (brand == null) {
            return "/error";
        }
        model.addAttribute("brand", brand);

        // 系列信息
        List<ProductSet> setList = productSetService.listSetsByBrandId(brandId);
        model.addAttribute("setList", setList);

        // 展厅信息
        List<Hall> hallList = hallService.listHallByBrandId(brandId);
        model.addAttribute("hallList", hallList);

        // 类目信息
        List<Category> catList = categoryDao.listAllCategoryByBrand(brandId);
        Map<String, List<Category>> catmap = new HashMap<String, List<Category>>();
        List<Category> list = null;
        for (Category cat : catList) {
            if (cat == null || cat.getIsParent() == null) {
                continue;
            }
            if (cat.getIsParent() == 1) {
                list = new ArrayList<Category>();
                catmap.put(cat.getName(), list);
                continue;
            } else {
                list.add(cat);
            }
        }
        model.addAttribute("catmap", catmap);

        // 推荐商品
        List<Product> recProductList = productService.getRecommendedProductInBrand(brandId);
        model.addAttribute("recProductList", recProductList);

        // 商品列表
        if (pageIndex == null) {
            pageIndex = 1;
        }
        Integer start = (pageIndex - 1) * pageSize;
        Page<Product> productList = productService.getProductInBrandByCateAndPSet(brandId, categoryId, productSetId,
                                                                                  start, pageSize);
        model.addAttribute("productList", productList);
        model.addAttribute("categoryId", categoryId);
        model.addAttribute("productSetId", productSetId);

        return "/brand/index";
    }
}
